<?php
/**
Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/

header('Content-Type: text/xml');
header('Cache-control: no-cache');

$module = $_GET['module'];
$page = $_GET['page'];
$user = $_GET['user'];
$checked = $_GET['checked']; //the checked is no requried

$project_home = $_GET['project_home'];

$conf_dir = $project_home."/store/other/plug_ins/Auth/";
$conf_file = $conf_dir."conf.xml";

$doc = new DOMDocument();
if(file_exists($conf_file))
{
	$doc->load($conf_file);
}
else
{
	if(!is_dir($project_home."/store"))
	{
		mkdir($project_home."/store");
	}
	if(!is_dir($project_home."/store/other"))
	{
		mkdir($project_home."/store/other");
	}
	if(!is_dir($project_home."/store/other/plug_ins"))
	{
		mkdir($project_home."/store/other/plug_ins");
	}
	if(!is_dir($project_home."/store/other/plug_ins/Auth/"))
	{
		mkdir($project_home."/store/other/plug_ins/Auth/");
	}
	$root = $doc->createElement('login');
	$doc-> appendChild($root);
}

$root = $doc->documentElement;

$module_found = false;
$module_elements = $root->childNodes;

foreach($module_elements as $module_element)
{
	$module_name = $module_element->getAttribute("name");
	if($module_name != $module)
	{
		continue;
	}
	$module_found = true;
	
	$page_found = false;
	$page_elements = $module_element->childNodes;
	foreach($page_elements as $page_element)
	{
		$page_name = $page_element->getAttribute("name");
		if($page_name != $page)
		{
			continue;
		}
		$page_found = true;
		
		$access_found = false;
		$access_elements = $page_element->childNodes;
		foreach($access_elements as $access_element)
		{
			$access_name = $access_element->getAttribute("name");
			if($access_name != $user)
			{
				continue;
			}
			$access_found = true;
			//so remove it.. since this should be the removing action
			$page_element->removeChild($access_element);
		}
		if(!$access_found)
		{
			$access_element = $doc->createElement("access");
			$access_element->setAttribute("name", $user);
			$page_element->appendChild($access_element);	
		}
	}
	if(!$page_found)
	{
		$page_element = $doc->createElement("page");
		$page_element->setAttribute("name", $page);
		$module_element->appendChild($page_element);
		
		$access_element = $doc->createElement("access");
		$access_element->setAttribute("name", $user);
		$page_element->appendChild($access_element);	
	}
}

if(!$module_found)
{
	$module_element = $doc->createElement("module");
	$module_element->setAttribute("name", $module);
	$root->appendChild($module_element);
	
	$page_element = $doc->createElement("page");
	$page_element->setAttribute("name", $page);
	$module_element->appendChild($page_element);
	
	$access_element = $doc->createElement("access");
	$access_element->setAttribute("name", $user);
	$page_element->appendChild($access_element);
	
}

$doc->save($conf_file);
	
?>
